package util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

//日期工具类
public class DateUtil {
	public final static String FORMAT1 = "yyyy-MM-dd";
	public final static String FORMAT2 = "yyyy-MM-dd HH:mm:ss";

	//根据格式将字符串转换成日期对象
	public static Date stringToDate(String dateStr, String formatStr) throws Exception {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			throw new Exception("failed to parese date");
		}

		return date;
	}
	
	public static Date stringToDate(String dateStr) throws Exception {
		return stringToDate(dateStr, FORMAT2);
	}
	
	public static String dateToString(Date date) {
		return dateToString(date, FORMAT2);
	}
	
	//根据格式将日期对象转换成字符串
	public static String dateToString(Date date, String formatStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(date);
	}
	
	public static String timestampToString(Timestamp ts) {
		return timestampToString(ts, FORMAT2);
	}
	
	public static String timestampToString(Timestamp ts, String formatStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		return sdf.format(ts);
	}
	
	public static Timestamp stringToTimestamp(String dateStr) {
		//SimpleDateFormat df = new SimpleDateFormat(FORMAT2);
		Timestamp ts = Timestamp.valueOf(dateStr);
		return ts;
	}
	
	public static Timestamp stringToTimestamp(String dateStr, String formatStr) {
		//SimpleDateFormat df = new SimpleDateFormat(formatStr);
		Timestamp ts = Timestamp.valueOf(dateStr);
		return ts;
	}
	
	public static String longToString(long date, String formatStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		java.util.Date dt = new Date(date);
		String sDateTime = sdf.format(dt);
		return sDateTime;
	}

	public static long stringToLong(String date, String formatStr)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		long millisecond = sdf.parse(date).getTime();
		return millisecond;
	}
	
	public static Date usToDate(String dateStr) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("E MMM dd hh:mm:ss z yyyy", Locale.US);
		Date date = format.parse(dateStr);
		return date;
	}
	
	/**
	 * 计算两个日期的时间差
	 * 
	 * @param formatTime1
	 * @param formatTime2
	 * @return
	 */
	public static long getTimeDifference(Date date1, Date date2) {
		long t1 = date1.getTime();
		long t2 = date2.getTime();
		return t1 - t2; //毫秒
	}
	
	/**
	 * 计算两个日期的时间差
	 * 
	 * @param formatTime1
	 * @param formatTime2
	 * @return
	 */
	public static long getTimeDifference(Timestamp formatTime1,
			Timestamp formatTime2) {
		SimpleDateFormat timeformat = new SimpleDateFormat(FORMAT2);
		long t1 = 0L;
		long t2 = 0L;
		try {
			t1 = timeformat.parse(getTimeStampNumberFormat(formatTime1))
					.getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			t2 = timeformat.parse(getTimeStampNumberFormat(formatTime2))
					.getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return t1 - t2; //毫秒
	}
	
	public static long getSecondDifference(Timestamp formatTime1,
			Timestamp formatTime2) {
		long l = getTimeDifference(formatTime1, formatTime2);
		int hours = (int) (l / 1000);
		return hours; //秒
	}
	
	public static long getSecondDifference(Date date1, Date date2) {
		long l = getTimeDifference(date1, date2);
		int hours = (int) (l / 1000);
		return hours; //秒
	}
	
	public static long getMiniDifference(Timestamp formatTime1,
			Timestamp formatTime2) {
		long l = getTimeDifference(formatTime1, formatTime2);
		int hours = (int) (l / (1000*60));
		return hours; //分钟
	}
	
	public static long getMiniDifference(Date date1, Date date2) {
		long l = getTimeDifference(date1, date2);
		int hours = (int) (l / (1000*60) );
		return hours; //date1
	}
	
	public static long getHourDifference(Timestamp formatTime1,
			Timestamp formatTime2) {
		long l = getTimeDifference(formatTime1, formatTime2);
		int hours = (int) (l / 3600000);
		return hours; //小时
	}
	
	public static long getHourDifference(Date date1, Date date2) {
		long l = getTimeDifference(date1, date2);
		int hours = (int) (l / 3600000 );
		return hours; //小时
	}
	
	/**
	 * 格式化时间 
	 * Locale是设置语言敏感操作
	 * 
	 * @param formatTime
	 * @return
	 */
	public static String getTimeStampNumberFormat(Timestamp formatTime) {
		SimpleDateFormat m_format = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss",
				new Locale("zh", "cn"));
		return m_format.format(formatTime);
	}

	public static void main(String[] args) {
		long l = new Date().getTime();
		String str = longToString(l , FORMAT2);
		System.out.println(str);
	}

}
